﻿namespace ExamV5.ManagSys
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class ManagDeptList : Page
    {
        protected Button butDefault;
        protected Button butDel;
        protected DropDownList DDLDept;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private HttpCookie httpCookie_4 = null;
        protected Label labID;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected TreeView TreeDept;
        protected TextBox txtDeptName;
        protected TextBox txtSortInfo;
        protected UpdatePanel UPPart;

        protected void butDefault_Click(object sender, EventArgs e)
        {
            if (this.ViewState["UserPower"].ToString() == "Y")
            {
                if (!((this.labID.Text.Trim() != "0") ? !(this.labID.Text == this.DDLDept.SelectedItem.Value) : true))
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('上级单位/部门名称不可以为当前单位/部门名称！')", true);
                }
                else
                {
                    string strSql = "";
                    if (!(this.labID.Text == "0"))
                    {
                        strSql = "update DeptInfo set SortInfo='" + this.txtSortInfo.Text + "',DeptName='" + this.publicFun_0.CheckString(this.txtDeptName.Text.Trim()) + "',UPID='" + this.DDLDept.SelectedItem.Value + "' where ID=" + this.labID.Text + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('编辑[" + this.publicFun_0.CheckString(this.txtDeptName.Text) + "]部门信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')";
                    }
                    else
                    {
                        strSql = "Insert into DeptInfo(SortInfo,DeptName,UPID,DeptLevel) values('" + this.txtSortInfo.Text + "','" + this.publicFun_0.CheckString(this.txtDeptName.Text.Trim()) + "','" + this.DDLDept.SelectedItem.Value + "','0');insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('新增[" + this.publicFun_0.CheckString(this.txtDeptName.Text) + "]部门信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')";
                    }
                    this.publicFun_0.ExecuteSql(strSql);
                    this.txtDeptName.Text = "";
                    this.DDLDept.SelectedIndex = -1;
                    this.labID.Text = "0";
                    this.TreeDept.Nodes.Clear();
                    TreeNode node = new TreeNode();
                    if (!(this.httpCookie_3.Value == "C"))
                    {
                        this.method_0(this.httpCookie_2.Value, node, this.httpCookie_4.Value, "0");
                    }
                    else
                    {
                        this.method_0("单位部门信息管理", node, "0", "0");
                    }
                    this.butDefault.Text = "新 增";
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('编辑单位/部门信息操作成功！')", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('用户管理权限未开放！')", true);
            }
        }

        protected void butDel_Click(object sender, EventArgs e)
        {
            if (!(this.ViewState["UserPower"].ToString() == "Y"))
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('用户管理权限未开放！')", true);
            }
            else if (!(this.labID.Text.Trim() != "0"))
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('请选择需要删除的单位/部门信息！')", true);
            }
            else if (!(this.publicFun_0.GetValues("select ID from UserInfo where DeptID=" + this.labID.Text.Trim(), "ID") == "") || !(this.publicFun_0.GetValues("select ID from DeptInfo where UPID=" + this.labID.Text.Trim(), "ID") == ""))
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此单位/部门中还存在人员或子部门信息,无法删除！')", true);
            }
            else
            {
                this.publicFun_0.ExecuteSql("Delete from DeptInfo where ID=" + this.labID.Text.Trim() + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('删除[" + this.publicFun_0.CheckString(this.txtDeptName.Text) + "]部门信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')");
                this.txtDeptName.Text = "";
                this.DDLDept.SelectedIndex = -1;
                this.labID.Text = "0";
                this.TreeDept.Nodes.Clear();
                TreeNode node = new TreeNode();
                if (this.httpCookie_3.Value == "C")
                {
                    this.method_0("单位部门信息管理", node, "0", "0");
                }
                else
                {
                    this.method_0(this.httpCookie_2.Value, node, this.httpCookie_4.Value, "0");
                }
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('删除单位/部门信息操作成功！')", true);
            }
        }

        private void method_0(string string_0, TreeNode treeNode_0, string string_1, string string_2)
        {
            TreeNode child = new TreeNode {
                Value = string_1,
                Text = string_0,
                ToolTip = string_2
            };
            if ((string_2 == "0") || (string_0 == "单位部门信息管理"))
            {
                this.TreeDept.Nodes.Add(child);
            }
            else
            {
                treeNode_0.ChildNodes.Add(child);
                treeNode_0.Expanded = false;
            }
            string cmdText = "select * from DeptInfo where UPID='" + string_1 + "' order by SortInfo";
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlCommand command = new SqlCommand(cmdText, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                this.method_0(reader["DeptName"].ToString(), child, reader["ID"].ToString(), reader["UPID"].ToString());
            }
            reader.Close();
            connection.Close();
            connection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_3 = new HttpCookie("ExamUserLevel");
            this.httpCookie_3 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_4 = new HttpCookie("ExamDeptID");
            this.httpCookie_4 = base.Request.Cookies["ExamDeptID"];
            this.httpCookie_2 = new HttpCookie("ExamDeptName");
            this.httpCookie_2 = base.Request.Cookies["ExamDeptName"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.httpCookie_4.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_4.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() == "Yes")
                    {
                        this.ViewState["UserPower"] = this.publicFun_0.GetValues("select ManagUser from UserPower where UserCode='" + this.httpCookie_0.Value + "'", "ManagUser");
                        this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString;
                        this.publicFun_0.LoadDeptInfo(this.DDLDept, this.httpCookie_4.Value, this.httpCookie_0.Value, this.httpCookie_3.Value);
                        if (this.httpCookie_3.Value.Trim() == "C")
                        {
                            this.DDLDept.SelectedIndex = -1;
                            ListItem item = new ListItem("顶级单位/部门", "0");
                            this.DDLDept.Items.Insert(0, item);
                        }
                        this.TreeDept.Nodes.Clear();
                        TreeNode node = new TreeNode();
                        if (this.httpCookie_3.Value == "C")
                        {
                            this.method_0("单位部门信息管理", node, "0", "0");
                        }
                        else
                        {
                            this.method_0(this.httpCookie_2.Value, node, this.httpCookie_4.Value, "0");
                        }
                        this.butDel.Attributes.Add("onclick", "javascript:{if(confirm('确定要删除吗？')==false) return false;}");
                        this.butDefault.Text = "新 增";
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }

        protected void TreeDept_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode selectedNode = this.TreeDept.SelectedNode;
            if (selectedNode.Text != "单位部门信息管理")
            {
                this.txtDeptName.Text = selectedNode.Text;
                this.DDLDept.SelectedIndex = -1;
                this.DDLDept.Items.FindByValue(selectedNode.ToolTip).Selected = true;
                this.labID.Text = selectedNode.Value;
                this.txtSortInfo.Text = this.publicFun_0.GetValues("select SortInfo from DeptInfo where ID='" + selectedNode.Value.Trim() + "'", "SortInfo");
                this.butDefault.Text = "修 改";
            }
            else
            {
                this.txtDeptName.Text = "";
                this.DDLDept.SelectedIndex = -1;
                this.DDLDept.SelectedIndex = 0;
                this.labID.Text = "0";
                this.txtSortInfo.Text = "";
                this.butDefault.Text = "新 增";
            }
        }
    }
}

